package com.fxpc.util;

import com.fxpc.common.vo.DivisionParamVo;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Zyl
 * @Date 2021/4/13 15:18
 * @Version 1.0
 * @Describe: jpa动态查询时组装查询条件的工具类
 */
public class GetSpecificationUtil {
    /**
     * 获取查询条件的组装
     *
     * @param divisionParam
     * @param <T>
     * @return
     */
    public static <T> Specification<T> getSpecification(DivisionParamVo divisionParam, boolean isPg) {
        Long total = 0L;
        Specification<T> countCondition = new Specification<T>() {
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (divisionParam.getProvince() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("province"), divisionParam.getProvince()));
                }
                if (divisionParam.getCity() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("city"), divisionParam.getCity()));
                }
                if (divisionParam.getArea() != null) {
                    if (isPg) {
                        predicateList.add(criteriaBuilder.equal(root.get("district"), divisionParam.getArea()));
                    } else {
                        predicateList.add(criteriaBuilder.equal(root.get("area"), divisionParam.getArea()));
                    }
                }
                predicateList.add(criteriaBuilder.equal(root.get("isValid"), "1"));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return countCondition;
    }

    /**
     * 获取查询条件的组装,yh_disaster表，区分房屋，市政，城市自来水的类型
     *
     * @param divisionParam
     * @param <T>
     * @return
     */
    public static <T> Specification<T> getSpecification1(DivisionParamVo divisionParam) {
        Specification<T> countCondition = new Specification<T>() {
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (divisionParam.getProvince() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("province"), divisionParam.getProvince()));
                }
                if (divisionParam.getCity() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("city"), divisionParam.getCity()));
                }
                if (divisionParam.getArea() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("area"), divisionParam.getArea()));
                }

                if (divisionParam.getExtends20() != null) {//hddc_d_resultmaptable表
                    predicateList.add(criteriaBuilder.equal(root.get("extends20"), divisionParam.getExtends20()));
                    predicateList.add(criteriaBuilder.isNotNull(root.get("attachId")));
                }
                if (divisionParam.getType() != null) {//fx_picture表
                    predicateList.add(criteriaBuilder.equal(root.get("type"), divisionParam.getType()));
                    predicateList.add(criteriaBuilder.isNotNull(root.get("attachId")));
                }
                if (divisionParam.getExtend10() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("extend10"), divisionParam.getExtend10()));
                }
                predicateList.add(criteriaBuilder.equal(root.get("isValid"), "1"));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return countCondition;
    }

    public static <T> Specification<T> getSpecification2(DivisionParamVo divisionParam, int flag) {
        Specification<T> countCondition = new Specification<T>() {
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (divisionParam.getProvince() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("province"), divisionParam.getProvince()));
                }
                if (divisionParam.getCity() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("city"), divisionParam.getCity()));
                }
                if (divisionParam.getArea() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("area"), divisionParam.getArea()));
                }
                if (divisionParam.getYhProjectType() != null) {//yh_disaster表
                    String yhProjectType = divisionParam.getYhProjectType();
                    Predicate predicate = null;
                    if (flag == 3) {
                        predicate = criteriaBuilder.like(root.get("yhProjectType"), "%"+yhProjectType+"%");
                    } else if (flag == 2) {
                        predicate = criteriaBuilder.equal(root.get("yhProjectType"), yhProjectType);
                    } else {
                        predicate = root.get("yhProjectType").in(yhProjectType.split(","));
                    }
                    predicateList.add(predicate);
                }
                predicateList.add(criteriaBuilder.equal(root.get("isValid"), "1"));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return countCondition;
    }

    public static <T> Specification<T> getSpecificationPicture(DivisionParamVo divisionParam) {
        Specification<T> countCondition = new Specification<T>() {
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (divisionParam.getProvince() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("province"), divisionParam.getProvince()));
                }
                if (divisionParam.getCity() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("city"), divisionParam.getCity()));
                }
                if (divisionParam.getArea() != null) {
                    predicateList.add(criteriaBuilder.equal(root.get("area"), divisionParam.getArea()));
                }
                if (divisionParam.getType() != null) {//fx_picture表
                    predicateList.add(criteriaBuilder.equal(root.get("type"), divisionParam.getType()));
                    predicateList.add(criteriaBuilder.isNotNull(root.get("attachId")));
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        return countCondition;
    }
}
